package org.springboot.szsrcb.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Attachment;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
//@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public List<User> getUser(){
        logger.info("从数据库读取Student集合");
        return userService.getList();
    }
        
    
    @RequestMapping(value = "/person_info_save")
	public boolean person_info_save(HttpServletRequest request,Model model,User user) {
		logger.info("person_info_save");
		// model.addAttribute("name", name);
		model.addAttribute("User",user);
		boolean r=false;
		if(user!=null){
			r = userService.change_rtx_account(user.getLogin_name(), user.getRtx_account());
			
		}
		
		return r;
	}
    
	@RequestMapping(value = "/person_info_form_load")
	public String person_info_form_load(HttpServletRequest request,HttpSession httpSession, Model model) {
		logger.info("person_info_form_load");
		// model.addAttribute("name", name);
		String login_name=(String) httpSession.getAttribute("login_name");
		String r="";
		if(login_name!=null){
			User u = userService.search_a_user(login_name);
			r=trans_2_json_person_info_form_load(u);
		}
		
		return r;
	}
	
	private String trans_2_json_person_info_form_load(User u) {
		String rtx="";
		if(u.getRtx_account()!=null){
			rtx=u.getRtx_account();
		}
		String r_start="["
				+ "{type: 'fieldset', label:'个人用户信息维护', list:["
								+"{type:'block',list:["						
									+"{type : 'input',name : 'user_id',label : 'user_id',labelWidth:'80',inputWidth:'150',position:'label-left', hidden:'true', offsetLeft:'25',value:'"+u.getId()+"'},"
									+"{type : 'input',name : 'name',label : '姓名',labelWidth:'80',inputWidth:'150',position:'label-left', disabled:'true', offsetLeft:'25',value:'"+u.getName()+"'},"											
									+"{type : 'input',name : 'login_name',label : '登录名',labelWidth:'80',inputWidth:'150',position:'label-left', disabled:'true', offsetLeft:'25',value:'"+u.getLogin_name()+"'},"
									+"{type : 'input',name : 'rtx_account',label : 'RTX账号',labelWidth:'80',inputWidth:'150',position:'label-left', required:'true', offsetLeft:'25',value:'"+rtx+"'},"
									//+"{type : 'input',name : 'pwd',label : '密码',labelWidth:'80',inputWidth:'150',position:'label-left', disabled:'true', offsetLeft:'25'},"
									
								+"]},"
								+"{type:'block',list:["
									+"{type : 'button',name : 'save_btn',value : '保存信息'},"																				
								+"]}"										
																	
							+"]"
					+ "}"
				+"]";
		String r_s=r_start;
		logger.info("retrun info :" + r_start);
		return r_s;
	}

}
